package com.ip.student.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ip.common.constant.Result;
import com.ip.common.enums.CodeEnum;
import com.ip.common.utils.StrUtils;
import com.ip.user.models.common.annotation.RequiresPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import com.ip.student.models.service.IStudentPunishmentService;
import com.ip.student.models.bo.StudentPunishmentSaveBO;
import com.ip.student.models.bo.StudentPunishmentBO;
import com.ip.student.models.dto.StudentPunishmentDTO;

/**
 * 学生处分信息控制器
 *
 * @author 张景奇
 * @Date 2025-11-17 10:16:07
 */

@RestController
@RequestMapping("/cms/studentPunishment")
@Slf4j
@Api(value = "StudentPunishmentController",tags = "学生处分信息接口")
public class StudentPunishmentController {

    @DubboReference
    private IStudentPunishmentService studentPunishmentService;

    @ApiOperation(value = "添加学生处分信息",notes = "添加学生处分信息")
    @PostMapping(value = "/insert")
    @RequiresPermission("student:punishment:add")
    public Result insert(@RequestBody StudentPunishmentSaveBO saveBO) {
        boolean flag = studentPunishmentService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存学生处分信息",notes = "保存学生处分信息")
    @RequiresPermission("student:punishment:add")
    public Result save(@RequestBody StudentPunishmentSaveBO saveBO){
        if(saveBO.getPunishId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改学生处分信息",notes = "修改学生处分信息")
    @PostMapping("/update")
    @RequiresPermission("student:punishment:update")
    public Result update(@RequestBody StudentPunishmentSaveBO saveBO){
        StudentPunishmentDTO dto = new StudentPunishmentDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setPunishId(saveBO.getPunishId());
        boolean flag = studentPunishmentService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除学生处分信息",notes = "删除学生处分信息")
    @ApiImplicitParam(name = "id",value = "学生处分信息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    @RequiresPermission("student:punishment:delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = studentPunishmentService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除学生处分信息",notes = "批量删除学生处分信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        studentPunishmentService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<StudentPunishmentDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        StudentPunishmentDTO dto = studentPunishmentService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "学生处分信息列表",notes = "学生处分信息列表")
    @PostMapping("/queryPageList")
    @RequiresPermission("student:punishment:query")
    public Result<Page<StudentPunishmentDTO>> queryPageList(@RequestBody StudentPunishmentBO bo){
        Wrapper<StudentPunishmentDTO> wrapper = new EntityWrapper<>();

        if (bo.getStudentId() != null){
            wrapper.eq("student_id", bo.getStudentId());
        }
        if (bo.getSchoolYear() != null){
            wrapper.eq("school_year", bo.getSchoolYear());
        }
        if (bo.getSemester() != null){
            wrapper.eq("semester", bo.getSemester());
        }
        if (bo.getGrade() != null){
            wrapper.eq("grade", bo.getGrade());
        }
        if (bo.getIsCanceled() != null){
            wrapper.eq("is_canceled", bo.getIsCanceled());
        }

        Page<StudentPunishmentDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = studentPunishmentService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
